# -*- coding: utf-8 -*-
import random
import click
from faker import Faker
from myweb import db, app
from myweb.models import Customer
from sqlalchemy.exc import IntegrityError
fake = Faker('zh_CN')


@app.cli.command()
def mywebinitdb():
    """Generate fake data."""
    db.drop_all()
    db.create_all()
    for i in range(10):
        customer = Customer()
        customer.title = fake.text(10)
        customer.detail = fake.text(60)
        customer.time = fake.date(pattern="%Y-%m-%d", end_datetime=None)
        customer.statu = random.randint(0, 1)
        db.session.add(customer)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()

    click.echo('Done.')

